﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public partial class admin_cms_Modul : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            loadropcha();

            loadgricha();
            loadgridcon();
        }
    }
    protected void loadropcha()
    {
        DataTable dtdr = new DataTable();
        dtdr = DataAccess.ExeSQLToDataTable("select * from admin_module_page where idcheck=0");
        int n = dtdr.Rows.Count;
        if (n > 0)
        {
            drl_modul_cha.DataSource = dtdr;
            drl_modul_cha.DataValueField = "idmodule";
            drl_modul_cha.DataTextField = "name_module";
            drl_modul_cha.DataBind();
        }
    }
    protected void insertmodulcha(object sender, EventArgs e)
    {
        string namecha = txtcha.Text;
        int tenht =Convert.ToInt32(txtht.Text);
        int n = 0;
        string filename = FileUpload1.FileName;
        if (filename != "")
        {
            FileUpload1.SaveAs(Server.MapPath("~/admin_cms/style/images/icons/small/grey/" + filename));
            DataAccess.ExeNonSQL("insert into admin_module_page(name_module,idcheck,picture,sort) values(N'" + namecha + "','" + n + "','" + filename + "','"+tenht+"')");
            loadgricha();
        }


    }
    protected void insertmodulcon(object sender, EventArgs e)
    {
        string namecon = txtcon.Text;
        string linkpage = txtlink.Text;
        string idcheck = drl_modul_cha.SelectedItem.Value.ToString();

        DataAccess.ExeNonSQL("insert into admin_module_page(name_module,idcheck,link_page) values(N'" + namecon + "','" + idcheck + "','" + linkpage + "')");
        loadgridcon();

    }
    protected void loadgricha()
    {
        int n = 0;
        string sql = "select * from admin_module_page where idcheck='" + n + "' order by sort desc";
        DataTable dt = new DataTable();
        dt = DataAccess.ExeSQLToDataTable(sql);
        gricha.DataSource = dt;
        gricha.DataBind();

    }
    protected void loadgridcon()
    {
        int n = 0;
        string sqlcon = "select * from admin_module_page where idcheck !='" + n + "'";
        DataTable dtcon = new DataTable();
        dtcon = DataAccess.ExeSQLToDataTable(sqlcon);
        if (dtcon.Rows.Count > 0)
        {
            GriviewCon.DataSource = dtcon;
            GriviewCon.DataBind();
        }
    }
    protected void grv_module_click(object sender, CommandEventArgs e)
    {

        string idmodule = e.CommandArgument.ToString();
        string CheckName = e.CommandName.ToString();
        if (CheckName == "X")
        {

            DataAccess.ExeNonSQL("delete from admin_roles where idmodule='" + idmodule + "'");
            DataAccess.ExeNonSQL("delete from admin_module_page where idmodule='" + idmodule + "'");
            Response.Write("<script>alert('Bạn đã xóa thành công !')</script>");
            loadgricha();
        }
        else
        {
            DataTable dtgrv_yahoo = new DataTable();
            dtgrv_yahoo = DataAccess.ExeSQLToDataTable("select * from admin_module_page where idmodule='" + idmodule + "'");
            if (dtgrv_yahoo.Rows.Count > 0)
            {
                try
                {
                    txtcha.Text = dtgrv_yahoo.Rows[0][1].ToString();
                }
                catch (Exception ex)
                {

                }
                finally
                {

                }
                Session.Add("idmodule", idmodule);
            }
        }
    }
    protected void grv_module_con_click(object sender, CommandEventArgs e)
    {

        string idmodule = e.CommandArgument.ToString();
        string CheckName = e.CommandName.ToString();
        if (CheckName == "X")
        {

            DataAccess.ExeNonSQL("delete from admin_roles where idmodule='" + idmodule + "'");
            DataAccess.ExeNonSQL("delete from admin_module_page where idmodule='" + idmodule + "'");
            
            loadgridcon();
        }
        else
        {
            DataTable dtgrv_yahoo = new DataTable();
            dtgrv_yahoo = DataAccess.ExeSQLToDataTable("select * from admin_module_page where idmodule='" + idmodule + "'");
            if (dtgrv_yahoo.Rows.Count > 0)
            {
                try
                {
                    txtcon.Text = dtgrv_yahoo.Rows[0][1].ToString();
                    txtlink.Text = dtgrv_yahoo.Rows[0][3].ToString();
                }
                catch (Exception ex)
                {

                }
                finally
                {

                }
                Session.Add("idmodule", idmodule);
            }
        }
    }
    protected void updatemodulecha(object sender, EventArgs e)
    {
        string namecha = txtcha.Text; int tenht = Convert.ToInt32(txtht.Text);
        string filename = FileUpload1.FileName;
        string idmodule = Session["idmodule"].ToString();
        if (filename != "")
        {
            FileUpload1.SaveAs(Server.MapPath("~/admin_cms/style/images/icons/small/grey/" + filename));
            DataAccess.ExeNonSQL("update admin_module_page set name_module=N'" + namecha + "',sort='"+tenht+"',picture='" + filename + "' where idmodule='" + idmodule + "'");
            loadgricha();
        }
        else
        {
            DataAccess.ExeNonSQL("update admin_module_page set name_module=N'" + namecha + "',sort='" + tenht + "' where idmodule='" + idmodule + "'");
            loadgricha();
        }
    }
    protected void updatetmodulcon(object sender, EventArgs e)
    {
        string namecon = txtcon.Text;
        string link = txtlink.Text; 
        string idcha = "";
        string idmodule = Session["idmodule"].ToString();
        
        idcha = drl_modul_cha.SelectedItem.Value.ToString();

        DataAccess.ExeNonSQL("update admin_module_page set name_module=N'" + namecon + "',link_page='" + link + "',idcheck='" + idcha + "' where idmodule='" + idmodule + "'");
        loadgridcon();
    }
}
